﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net;
using System.Text;
using System.Web;

namespace HelpNetwork
{
    class WebComm
    {
        private static int prelucrareAlarme(Principal.ActiveAlarms activeAlarms)
        {
            int alarme = 0;
            alarme += activeAlarms.alarmaLumina == true ? 1:0;
            alarme += activeAlarms.alarmaGaz == true? 2:0;
            alarme += activeAlarms.alarmaUmiditate == true ? 4:0;
            alarme += activeAlarms.alarmaRFID == true ? 8:0;
            alarme += activeAlarms.alarmaTensiune == true ? 16:0;

            return alarme;
        }
        public static void sendMessage(Principal.ActiveAlarms activeAlarms,string hostname)
        {
            HttpWebRequest request;
            HttpWebResponse response;
            string url = String.Concat(hostname, "/conexiuneIP.php");

            try
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                request.AllowAutoRedirect = true;
                response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    StringBuilder sb = new StringBuilder();
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    while (!reader.EndOfStream)
                    {
                        sb.AppendLine(reader.ReadLine());
                    }

                    String messageURL = url + "?alarms=" + prelucrareAlarme(activeAlarms).ToString() + "&HTA_diastolic=" + Principal.mainForm.appParam.parametriiCurenti.HTA.Diastolic.ToString() 
                                        + "&HTA_sistolic=" + Principal.mainForm.appParam.parametriiCurenti.HTA.Sistolic.ToString() + "&puls=" + Principal.mainForm.appParam.parametriiCurenti.puls.ToString()
                                        + "&temperatura=" + Principal.mainForm.appParam.parametriiCurenti.tempCorp.ToString() + "&greutate=" + Principal.mainForm.appParam.parametriiCurenti.greutate.ToString()
                                        + "&glicemie="+ Principal.mainForm.appParam.parametriiCurenti.glicemie.ToString() + "&frecventa=" + Principal.mainForm.appParam.parametriiCurenti.frecvResp.ToString()
                                        + "&temperaturaAmb=" + Principal.mainForm.appParam.parametriiCurenti.tempAmbientala.ToString() +"&username=" + "pacient"+ "&password=" + "test";

                    request = (HttpWebRequest)WebRequest.Create(messageURL);
                    response = (HttpWebResponse)request.GetResponse();

                    Principal.mainForm.inserareMesajStare("Dispeceratul a raspuns: " + response.StatusCode + Environment.NewLine);

                    StringBuilder output = new StringBuilder();

                    using (Stream s = response.GetResponseStream())
                    {
                        StreamReader sr = new StreamReader(s);
                        while (!sr.EndOfStream)
                        {
                            Principal.mainForm.inserareMesajStare(sr.ReadLine());
                        }
                    }
                    response.Close();
                }
                else
                {
                    Principal.mainForm.inserareMesajStare("Comunicarea cu dispeceratul a esuat. Verificati accesul la Internet!");
                }
            }
            catch (System.Exception e)
            {
                Principal.mainForm.inserareMesajStare("S-a generat eroarea: " + e.Message + " la comunicarea cu dispeceratul.\r\n");
            }

        }
    
    }
}
